From 2be4547f985e18d3c3b8c928144a0a0304666c2f Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Fri, 6 Jan 2023 05:56:06 -0700 Subject: [PATCH] remove remaining shadows of cet (#971) * wean garmin_gpi from global_opts.codec and cet. * wean garmin from global_opts.codec and cet. * remove cet * don't include deprecated includes, no global codec. * fix garmin includes * fix up comment indicating garmin encoding issues. * fiddle with encoding functions. * change get_codec signature. * trailling white space --- CMakeLists.txt | 2 - defs.h | 4 +- cet_util.cc => deprecated/cet_util.cc | 0 cet_util.h => deprecated/cet_util.h | 0 garmin.cc | 88 ++++++++++++++++++++++++--- garmin_fs.cc | 68 --------------------- garmin_fs.h | 4 -- garmin_gpi.cc | 67 ++++++++++---------- garmin_gpi.h | 6 +- gdb.cc | 9 --- igc.cc | 4 -- main.cc | 16 ----- nmea.cc | 5 -- util.cc | 9 +++ 14 files changed, 126 insertions(+), 156 deletions(-) rename cet_util.cc => deprecated/cet_util.cc (100%) rename cet_util.h => deprecated/cet_util.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c4448d50..0d4921501 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -165,7 +165,6 @@ set(JEEPS # SUPPORT set(SUPPORT - cet_util.cc csv_util.cc fatal.cc filter_vecs.cc @@ -203,7 +202,6 @@ endif() # HEADERS set(HEADERS - cet_util.h csv_util.h defs.h dg-100.h diff --git a/defs.h b/defs.h index fe74f4f74..c56bb162d 100644 --- a/defs.h +++ b/defs.h @@ -48,8 +48,6 @@ #define CSTR(qstr) ((qstr).toUtf8().constData()) #define CSTRc(qstr) ((qstr).toLatin1().constData()) -#define STRFROMUNICODE(qstr) (global_opts.codec->fromUnicode(qstr).constData()) -#define STRTOUNICODE(cstr) (global_opts.codec->toUnicode(cstr)) /* * Amazingly, this constant is not specified in the standard... @@ -182,7 +180,6 @@ struct global_options { int smart_icons; int smart_names; inifile_t* inifile; - QTextCodec* codec; }; extern global_options global_opts; @@ -1064,6 +1061,7 @@ void gb_setbit(void* buf, uint32_t nr); void* gb_int2ptr(int i); int gb_ptr2int(const void* p); +QTextCodec* get_codec(const QByteArray& cs_name); void list_codecs(); void list_timezones(); QString grapheme_truncate(const QString& input, unsigned int count); diff --git a/cet_util.cc b/deprecated/cet_util.cc similarity index 100% rename from cet_util.cc rename to deprecated/cet_util.cc diff --git a/cet_util.h b/deprecated/cet_util.h similarity index 100% rename from cet_util.h rename to deprecated/cet_util.h diff --git a/garmin.cc b/garmin.cc index 14166c3eb..0b1ba3b87 100644 --- a/garmin.cc +++ b/garmin.cc @@ -31,13 +31,14 @@ #include // for QByteArray #include // for QChar #include // for QString +#include // for QTextCodec #include // for QVector #include // for CaseInsensitive #include // for qPrintable, foreach #include "defs.h" -#include "cet_util.h" // for cet_convert_init, cet_cs_vec_utf8 #include "format.h" // for Format +#include "formspec.h" // for FormatSpecificDataList #include "garmin_device_xml.h" // for gdx_get_info, gdx_info, gdx_file, gdx_jmp_buf #include "garmin_fs.h" // for garmin_fs_garmin_after_read, garmin_fs_garmin_before_write #include "garmin_tables.h" // for gt_find_icon_number_from_desc, PCX, gt_find_desc_from_icon_number @@ -78,6 +79,7 @@ static char* baudopt = nullptr; static int baud = 0; static int categorybits; static int receiver_must_upper = 1; +static QTextCodec* codec{nullptr}; static Format* gpx_vec; @@ -129,13 +131,17 @@ QVector garmin_args = { static const char* d103_symbol_from_icon_number(unsigned int n); static int d103_icon_number_from_symbol(const QString& s); +static void garmin_fs_garmin_after_read(GPS_PWay way, Waypoint* wpt, int protoid); +static void garmin_fs_garmin_before_write(const Waypoint* wpt, GPS_PWay way, int protoid); +static QByteArray str_from_unicode(const QString& qstr) {return codec->fromUnicode(qstr);} +static QString str_to_unicode(const QByteArray& cstr) {return codec->toUnicode(cstr);} static void rw_init(const QString& fname) { receiver_must_upper = 1; - const char* receiver_charset = nullptr; + const char* receiver_charset = CET_CHARSET_ASCII; if (!mkshort_handle) { mkshort_handle = mkshort_new_handle(); @@ -335,15 +341,13 @@ rw_init(const QString& fname) /* * This used to mean something when we used cet, but these days this - * format either use implicit QString conversions (utf8) which is - * likely a bug, or we have hard coded QString::fromLatin1 or CSTRc. - * So all the above detection of receiver_charset is for naught. - * But perhaps we will use an appropriate codec based on receiver_charset - * someday. + * format either uses implicit QString conversions (utf8), + * or we have hard coded QString::fromLatin1, CSTRc, or CSTR. These + * are likely bugs. + * However, this is still used for garmin_fs_garmin_after_read, + * garmin_fs_garmin_before_write. */ - if (receiver_charset) { - cet_convert_init(receiver_charset, 1); - } + codec = get_codec(receiver_charset); } static void @@ -1210,3 +1214,67 @@ d103_icon_number_from_symbol(const QString& s) } return 0; } + +static void +garmin_fs_garmin_after_read(const GPS_PWay way, Waypoint* wpt, const int protoid) +{ + garmin_fs_t* gmsd = garmin_fs_alloc(protoid); + wpt->fs.FsChainAdd(gmsd); + + /* nothing happens until gmsd is allocated some lines above */ + + /* !!! class needs protocol specific conversion !!! (ToDo) + garmin_fs_t::set_wpt_class(gmsd, way[i]->wpt_class); + */ + /* flagged data fields */ + garmin_fs_t::set_display(gmsd, gt_switch_display_mode_value(way->dspl, gps_waypt_type, 1)); + if (way->category != 0) { + garmin_fs_t::set_category(gmsd, way->category); + } + if (way->dst < 1.0e25f) { + WAYPT_SET(wpt, proximity, way->dst); + } + if (way->temperature_populated) { + WAYPT_SET(wpt, temperature, way->temperature); + } + if (way->dpth < 1.0e25f) { + WAYPT_SET(wpt, depth, way->dpth); + } + /* will copy until a null character or the end of the fixed length way field is reached, whichever comes first. */ + garmin_fs_t::set_cc(gmsd, str_to_unicode(QByteArray(way->cc, qstrnlen(way->cc, sizeof(way->cc))))); + garmin_fs_t::set_city(gmsd, str_to_unicode(QByteArray(way->city, qstrnlen(way->city, sizeof(way->city))))); + garmin_fs_t::set_state(gmsd, str_to_unicode(QByteArray(way->state, qstrnlen(way->state, sizeof(way->state))))); + garmin_fs_t::set_facility(gmsd, str_to_unicode(QByteArray(way->facility, qstrnlen(way->facility, sizeof(way->facility))))); + garmin_fs_t::set_cross_road(gmsd, str_to_unicode(QByteArray(way->cross_road, qstrnlen(way->cross_road, sizeof(way->cross_road))))); + garmin_fs_t::set_addr(gmsd, str_to_unicode(QByteArray(way->addr, qstrnlen(way->addr, sizeof(way->addr))))); +} + +static void +garmin_fs_garmin_before_write(const Waypoint* wpt, GPS_PWay way, const int protoid) +{ + garmin_fs_t* gmsd = garmin_fs_t::find(wpt); + + (void)protoid; // unused for now. + + if (gmsd == nullptr) { + return; + } + + /* ToDo: protocol specific conversion of class + way[i]->wpt_class = garmin_fs_t::get_wpt_class(gmsd, way[i]->wpt_class); + */ + way->dspl = gt_switch_display_mode_value( + garmin_fs_t::get_display(gmsd, way->dspl), gps_waypt_type, 0); + way->category = garmin_fs_t::get_category(gmsd, way->category); + way->dpth = WAYPT_GET(wpt, depth, way->dpth); + way->dst = WAYPT_GET(wpt, proximity, way->dpth); + way->temperature = WAYPT_GET(wpt, temperature, way->temperature); + + /* destination may not be null terminated, but we will fill with nulls if necessary */ + strncpy(way->cc, str_from_unicode(garmin_fs_t::get_cc(gmsd, nullptr)).constData(), sizeof(way->cc)); + strncpy(way->city, str_from_unicode(garmin_fs_t::get_city(gmsd, nullptr)).constData(), sizeof(way->city)); + strncpy(way->state, str_from_unicode(garmin_fs_t::get_state(gmsd, nullptr)).constData(), sizeof(way->state)); + strncpy(way->facility, str_from_unicode(garmin_fs_t::get_facility(gmsd, nullptr)).constData(), sizeof(way->facility)); + strncpy(way->cross_road, str_from_unicode(garmin_fs_t::get_cross_road(gmsd, nullptr)).constData(), sizeof(way->cross_road)); + strncpy(way->addr, str_from_unicode(garmin_fs_t::get_addr(gmsd, nullptr)).constData(), sizeof(way->addr)); +} diff --git a/garmin_fs.cc b/garmin_fs.cc index 62108942c..753bc5395 100644 --- a/garmin_fs.cc +++ b/garmin_fs.cc @@ -20,12 +20,9 @@ */ -#include // for assert #include // for snprintf, sscanf #include // for strtod -#include // for strncpy -#include // for QByteArray #include // for QString, QStringLiteral #include // for QXmlStreamWriter #include // for CaseInsensitive @@ -34,7 +31,6 @@ #include "garmin_fs.h" #include "garmin_tables.h" // for gt_switch_display_mode_value, gt_display_mode_symbol, gt_display_mode_symbol_and_comment, gt_display_mode_symbol_and_name #include "inifile.h" // for inifile_readstr -#include "jeeps/gps.h" // for gps_waypt_type #define MYNAME "garmin_fs" @@ -320,67 +316,3 @@ garmin_fs_merge_category(const char* category_name, Waypoint* waypt) garmin_fs_t::set_category(gmsd, cat); return 1; } - -void -garmin_fs_garmin_after_read(const GPS_PWay way, Waypoint* wpt, const int protoid) -{ - garmin_fs_t* gmsd = garmin_fs_alloc(protoid); - wpt->fs.FsChainAdd(gmsd); - - /* nothing happens until gmsd is allocated some lines above */ - - /* !!! class needs protocol specific conversion !!! (ToDo) - garmin_fs_t::set_wpt_class(gmsd, way[i]->wpt_class); - */ - /* flagged data fields */ - garmin_fs_t::set_display(gmsd, gt_switch_display_mode_value(way->dspl, gps_waypt_type, 1)); - if (way->category != 0) { - garmin_fs_t::set_category(gmsd, way->category); - } - if (way->dst < 1.0e25f) { - WAYPT_SET(wpt, proximity, way->dst); - } - if (way->temperature_populated) { - WAYPT_SET(wpt, temperature, way->temperature); - } - if (way->dpth < 1.0e25f) { - WAYPT_SET(wpt, depth, way->dpth); - } - // These use STRTOUNICODE which uses gobal_opts.codec. - garmin_fs_t::set_cc(gmsd, STRTOUNICODE(QByteArray(way->cc, sizeof(way->cc)).constData())); - garmin_fs_t::set_city(gmsd, STRTOUNICODE(QByteArray(way->city, sizeof(way->city)).constData())); - garmin_fs_t::set_state(gmsd, STRTOUNICODE(QByteArray(way->state, sizeof(way->state)).constData())); - garmin_fs_t::set_facility(gmsd, STRTOUNICODE(QByteArray(way->facility, sizeof(way->facility)).constData())); - garmin_fs_t::set_cross_road(gmsd, STRTOUNICODE(QByteArray(way->cross_road, sizeof(way->cross_road)).constData())); - garmin_fs_t::set_addr(gmsd, STRTOUNICODE(QByteArray(way->addr, sizeof(way->addr)).constData())); -} - -void -garmin_fs_garmin_before_write(const Waypoint* wpt, GPS_PWay way, const int protoid) -{ - garmin_fs_t* gmsd = garmin_fs_t::find(wpt); - - (void)protoid; // unused for now. - - if (gmsd == nullptr) { - return; - } - - /* ToDo: protocol specific conversion of class - way[i]->wpt_class = garmin_fs_t::get_wpt_class(gmsd, way[i]->wpt_class); - */ - way->dspl = gt_switch_display_mode_value( - garmin_fs_t::get_display(gmsd, way->dspl), gps_waypt_type, 0); - way->category = garmin_fs_t::get_category(gmsd, way->category); - way->dpth = WAYPT_GET(wpt, depth, way->dpth); - way->dst = WAYPT_GET(wpt, proximity, way->dpth); - way->temperature = WAYPT_GET(wpt, temperature, way->temperature); - - // These use STRFROMUNICODE which uses gobal_opts.codec. - strncpy(way->cc, STRFROMUNICODE(garmin_fs_t::get_cc(gmsd, nullptr)), sizeof(way->cc)); - strncpy(way->city, STRFROMUNICODE(garmin_fs_t::get_city(gmsd, nullptr)), sizeof(way->city)); - strncpy(way->state, STRFROMUNICODE(garmin_fs_t::get_state(gmsd, nullptr)), sizeof(way->state)); - strncpy(way->facility, STRFROMUNICODE(garmin_fs_t::get_facility(gmsd, nullptr)), sizeof(way->facility)); - strncpy(way->cross_road, STRFROMUNICODE(garmin_fs_t::get_cross_road(gmsd, nullptr)), sizeof(way->cross_road)); - strncpy(way->addr, STRFROMUNICODE(garmin_fs_t::get_addr(gmsd, nullptr)), sizeof(way->addr)); -} diff --git a/garmin_fs.h b/garmin_fs.h index 13e8d4039..1d9fd348e 100644 --- a/garmin_fs.h +++ b/garmin_fs.h @@ -32,7 +32,6 @@ #include "defs.h" #include "formspec.h" // for FsChainFind, kFsGmsd, FormatSpecificData -#include "jeeps/gps.h" /* this order is used by most devices */ @@ -236,7 +235,4 @@ unsigned char garmin_fs_merge_category(const char* category_name, Waypoint* wayp #define GMSD_SECTION_CATEGORIES "Garmin Categories" -void garmin_fs_garmin_after_read(GPS_PWay way, Waypoint* wpt, int protoid); -void garmin_fs_garmin_before_write(const Waypoint* wpt, GPS_PWay way, int protoid); - #endif diff --git a/garmin_gpi.cc b/garmin_gpi.cc index aaa1e1fcb..8c2d3c8e4 100644 --- a/garmin_gpi.cc +++ b/garmin_gpi.cc @@ -38,8 +38,7 @@ #include // for time, gmtime, time_t, tm #include // for unique_ptr -#include "defs.h" // for Waypoint, fatal, STRFROMUNICODE, le_write32, le_write16, wp_flags, warning, bounds, KPH_TO_MPS, MPH_TO_MPS, WAYPT_HAS, gpsbabel_testmode, parse_speed, WAYPT_SET, MILES_TO_METERS, MPS_TO_KPH, MPS_TO_MPH, mkgmtime, mkshort, mkshort_del_handle, mkshort_new_... -#include "cet_util.h" // for cet_convert_init +#include "defs.h" // for Waypoint, fatal, le_write32, le_write16, wp_flags, warning, bounds, KPH_TO_MPS, MPH_TO_MPS, WAYPT_HAS, gpsbabel_testmode, parse_speed, WAYPT_SET, MILES_TO_METERS, MPS_TO_KPH, MPS_TO_MPH, mkgmtime, mkshort, mkshort_del_handle, mkshort_new_... #include "formspec.h" // for FormatSpecificDataList #include "garmin_fs.h" // for garmin_fs_t, garmin_fs_alloc #include "gbfile.h" // for gbfputint32, gbfgetint32, gbfgetint16, gbfputint16, gbfgetc, gbfputc, gbfread, gbftell, gbfwrite, gbfseek, gbfclose, gbfopen_le, gbfgetuint16, gbsize_t, gbfile @@ -149,7 +148,7 @@ GarminGPIFormat::gpi_read_string(const char* field) const } } - QString result = STRTOUNICODE(string).trimmed(); + QString result = str_to_unicode(string).trimmed(); if (GPI_DBG) { warning("%s: \"%s\"\n", field, result.isNull() ? "" : qPrintable(result)); } @@ -560,15 +559,15 @@ GarminGPIFormat::read_tag(const char* caller, const int tag, Waypoint* wpt) *******************************************************************************/ void -GarminGPIFormat::write_string(const char* str, const char long_format) const +GarminGPIFormat::write_string(const QByteArray& str, const char long_format) const { - int len = strlen(str); + int len = str.size(); if (long_format) { gbfputint32(len + 4, fout); gbfwrite("EN", 1, 2, fout); } gbfputint16(len, fout); - gbfwrite(str, 1, len, fout); + gbfwrite(str.constData(), 1, len, fout); } bool @@ -707,7 +706,7 @@ GarminGPIFormat::wdata_compute_size(writer_data_t* data) const res += 12; /* tag/sz/sub-sz */ res += 19; /* poi fixed size */ - res += strlen(STRFROMUNICODE(wpt->shortname)); + res += str_from_unicode(wpt->shortname).size(); if (! opt_hide_bitmap) { res += 10; /* tag(4) */ } @@ -764,33 +763,33 @@ GarminGPIFormat::wdata_compute_size(writer_data_t* data) const if (!str.isEmpty()) { dt->addr = str; dt->mask |= GPI_ADDR_ADDR; - dt->sz += (8 + strlen(STRFROMUNICODE(dt->addr))); + dt->sz += (8 + str_from_unicode(dt->addr).size()); } if ((gmsd = garmin_fs_t::find(wpt))) { if ((dt->mask == 0) && !(dt->addr = garmin_fs_t::get_addr(gmsd, nullptr)).isEmpty()) { dt->mask |= GPI_ADDR_ADDR; - dt->sz += (8 + strlen(STRFROMUNICODE(dt->addr))); + dt->sz += (8 + str_from_unicode(dt->addr).size()); } if (!(dt->city = garmin_fs_t::get_city(gmsd, nullptr)).isEmpty()) { dt->mask |= GPI_ADDR_CITY; - dt->sz += (8 + strlen(STRFROMUNICODE(dt->city))); + dt->sz += (8 + str_from_unicode(dt->city).size()); } if (!(dt->country = garmin_fs_t::get_country(gmsd, nullptr)).isEmpty()) { dt->mask |= GPI_ADDR_COUNTRY; - dt->sz += (8 + strlen(STRFROMUNICODE(dt->country))); + dt->sz += (8 + str_from_unicode(dt->country).size()); } if (!(dt->state = garmin_fs_t::get_state(gmsd, nullptr)).isEmpty()) { dt->mask |= GPI_ADDR_STATE; - dt->sz += (8 + strlen(STRFROMUNICODE(dt->state))); + dt->sz += (8 + str_from_unicode(dt->state).size()); } if (!(dt->postal_code = garmin_fs_t::get_postal_code(gmsd, nullptr)).isEmpty()) { dt->mask |= GPI_ADDR_POSTAL_CODE; - dt->sz += (2 + strlen(STRFROMUNICODE(dt->postal_code))); /* short form */ + dt->sz += (2 + str_from_unicode(dt->postal_code).size()); /* short form */ } if (!(dt->phone_nr = garmin_fs_t::get_phone_nr(gmsd, nullptr)).isEmpty()) { - res += (12 + 4 + strlen(STRFROMUNICODE(dt->phone_nr))); + res += (12 + 4 + str_from_unicode(dt->phone_nr).size()); } } if (dt->mask) { @@ -806,7 +805,7 @@ GarminGPIFormat::wdata_compute_size(writer_data_t* data) const } // if (str && (strcmp(str, wpt->shortname) == 0)) str = NULL; if (!str.isEmpty()) { - res += (12 + 4 + strlen(STRFROMUNICODE(str))); + res += (12 + 4 + str_from_unicode(str).size()); } } @@ -864,18 +863,18 @@ GarminGPIFormat::wdata_write(const writer_data_t* data) const } gbfputint32(0x80002, fout); - int s0 = s1 = 19 + strlen(STRFROMUNICODE(wpt->shortname)); + int s0 = s1 = 19 + str_from_unicode(wpt->shortname).size(); if (! opt_hide_bitmap) { s0 += 10; /* tag(4) */ } if (!str.isEmpty()) { - s0 += (12 + 4 + strlen(STRFROMUNICODE(str))); /* descr */ + s0 += (12 + 4 + str_from_unicode(str).size()); /* descr */ } if (dt->sz) { s0 += (12 + dt->sz); /* address part */ } if (!dt->phone_nr.isEmpty()) { - s0 += (12 + 4 + strlen(STRFROMUNICODE(dt->phone_nr))); + s0 += (12 + 4 + str_from_unicode(dt->phone_nr).size()); } if (dt->alerts) { s0 += 20; /* tag(3) */ @@ -890,7 +889,7 @@ GarminGPIFormat::wdata_write(const writer_data_t* data) const gbfputint16(1, fout); /* ? always 1 ? */ gbfputc(alerts, fout); /* seems to be 1 when extra options present */ - write_string(STRFROMUNICODE(wpt->shortname), 1); + write_string(str_from_unicode(wpt->shortname), 1); if (dt->alerts) { char flag = 0; @@ -926,8 +925,8 @@ GarminGPIFormat::wdata_write(const writer_data_t* data) const if (!str.isEmpty()) { gbfputint32(0xa, fout); - gbfputint32(strlen(STRFROMUNICODE(str)) + 8, fout); /* string + string header */ - write_string(STRFROMUNICODE(str), 1); + gbfputint32(str_from_unicode(str).size() + 8, fout); /* string + string header */ + write_string(str_from_unicode(str), 1); } if (dt->sz) { /* gpi address */ @@ -936,28 +935,28 @@ GarminGPIFormat::wdata_write(const writer_data_t* data) const gbfputint32(0x2, fout); /* ? always 2 ? */ gbfputint16(dt->mask, fout); if (dt->mask & GPI_ADDR_CITY) { - write_string(STRFROMUNICODE(dt->city), 1); + write_string(str_from_unicode(dt->city), 1); } if (dt->mask & GPI_ADDR_COUNTRY) { - write_string(STRFROMUNICODE(dt->country), 1); + write_string(str_from_unicode(dt->country), 1); } if (dt->mask & GPI_ADDR_STATE) { - write_string(STRFROMUNICODE(dt->state), 1); + write_string(str_from_unicode(dt->state), 1); } if (dt->mask & GPI_ADDR_POSTAL_CODE) { - write_string(STRFROMUNICODE(dt->postal_code), 0); + write_string(str_from_unicode(dt->postal_code), 0); } if (dt->mask & GPI_ADDR_ADDR) { - write_string(STRFROMUNICODE(dt->addr), 1); + write_string(str_from_unicode(dt->addr), 1); } } if (!dt->phone_nr.isEmpty()) { gbfputint32(0x8000c, fout); - gbfputint32(strlen(STRFROMUNICODE(dt->phone_nr)) + 2 + 2, fout); + gbfputint32(str_from_unicode(dt->phone_nr).size() + 2 + 2, fout); gbfputint32(0x2, fout); /* ? always 2 ? */ gbfputint16(1, fout); /* mask */ - write_string(STRFROMUNICODE(dt->phone_nr), 0); + write_string(str_from_unicode(dt->phone_nr), 0); } } @@ -982,7 +981,7 @@ GarminGPIFormat::write_category(const char* /*unused*/, const unsigned char* ima { int sz = wdata_compute_size(wdata); sz += 8; /* string header */ - sz += strlen(STRFROMUNICODE(QString::fromUtf8(opt_cat))); + sz += str_from_unicode(QString::fromUtf8(opt_cat)).size(); gbfputint32(0x80009, fout); if ((! opt_hide_bitmap) && image_sz) { @@ -991,7 +990,7 @@ GarminGPIFormat::write_category(const char* /*unused*/, const unsigned char* ima gbfputint32(sz, fout); } gbfputint32(sz, fout); - write_string(STRFROMUNICODE(QString::fromUtf8(opt_cat)), 1); + write_string(str_from_unicode(QString::fromUtf8(opt_cat)), 1); wdata_write(wdata); @@ -1229,10 +1228,10 @@ GarminGPIFormat::rd_init(const QString& fname) read_header(); if ((codepage >= 1250) && (codepage <= 1257)) { - QString qCodecName = QStringLiteral("windows-%1").arg(codepage); - cet_convert_init(CSTR(qCodecName), 1); + QByteArray qCodecName = "windows-" + QByteArray::number(codepage); + codec = get_codec(qCodecName); } else if (codepage == 65001) { - cet_convert_init("utf8", 1); + codec = get_codec("utf8"); } else { fatal(MYNAME ": Unsupported code page (%d). File is likely encrypted.\n", codepage); } @@ -1288,7 +1287,7 @@ GarminGPIFormat::wr_init(const QString& fname) fatal(MYNAME ": Valid values are windows-1250 to windows-1257 and utf8.\n"); } - cet_convert_init(opt_writecodec,1); + codec = get_codec(opt_writecodec); units = tolower(opt_units[0]); if ((units != 'm') && (units != 's')) { diff --git a/garmin_gpi.h b/garmin_gpi.h index ee9c32f48..a339d0e8b 100644 --- a/garmin_gpi.h +++ b/garmin_gpi.h @@ -49,6 +49,7 @@ #include // for QByteArray #include // for QList #include // for QString +#include // for QTextCodec #include // for QVector #include // for int32_t, int16_t, uint16_t @@ -297,7 +298,7 @@ private: void read_poi_list(int sz); void read_poi_group(int sz, int tag); int read_tag(const char* caller, int tag, Waypoint* wpt); - void write_string(const char* str, char long_format) const; + void write_string(const QByteArray& str, char long_format) const; static bool compare_wpt_cb(const Waypoint* a, const Waypoint* b); static char compare_strings(const QString& s1, const QString& s2); static writer_data_t* wdata_alloc(); @@ -310,6 +311,8 @@ private: void write_header() const; void enum_waypt_cb(const Waypoint* ref) const; static void load_bitmap_from_file(const char* fname, const unsigned char** data, int* data_sz); + QByteArray str_from_unicode(const QString& qstr) const {return codec->fromUnicode(qstr);} + QString str_to_unicode(const QByteArray& cstr) const {return codec->toUnicode(cstr);} /* Data Members */ @@ -387,6 +390,7 @@ private: short_handle short_h{}; char units{}; time_t gpi_timestamp = 0; + QTextCodec* codec{nullptr}; }; #endif // GARMIN_GPI_H_INCLUDED_ diff --git a/gdb.cc b/gdb.cc index de388f4b5..e205c2d55 100644 --- a/gdb.cc +++ b/gdb.cc @@ -40,7 +40,6 @@ #include // for next #include "defs.h" // for Waypoint, warning, fatal, route_head, UrlLink, bounds, mkshort, UrlList, unknown_alt, wp_flags, xfree, waypt_add_to_bounds, waypt_init_bounds, mkshort_del_handle, route_add_wpt, route_disp_all, waypt_bounds_valid, xmalloc, WAYPT_GET, WAYPT_SET, gb_color -#include "cet_util.h" // for cet_convert_init #include "formspec.h" // for FormatSpecificDataList #include "garmin_fs.h" // for garmin_fs_t, garmin_ilink_t, garmin_fs_alloc #include "garmin_tables.h" // for gt_waypt_class_map_point, gt_color_index_by_rgb, gt_color_value, gt_waypt_classes_e, gt_find_desc_from_icon_number, gt_find_icon_number_from_desc, gt_gdb_display_mode_symbol, gt_get_icao_country, gt_waypt_class_user_waypoint, GDB, gt_display_mode_symbol @@ -970,10 +969,6 @@ GdbFormat::rd_init(const QString& fname) fin = gbfopen_le(fname, "rb", MYNAME); ftmp = gbfopen_le(nullptr, "wb", MYNAME); read_file_header(); - /* VERSION DEPENDENT CODE */ - if (gdb_ver >= GDB_VER_UTF8) { - cet_convert_init(CET_CHARSET_UTF8, 1); - } wayptq_in.clear(); wayptq_in_hidden.clear(); @@ -1734,10 +1729,6 @@ GdbFormat::wr_init(const QString& fname) gdb_category = strtol(gdb_opt_bitcategory, nullptr, 0); } - if (gdb_ver >= GDB_VER_UTF8) { - cet_convert_init(CET_CHARSET_UTF8, 1); - } - wayptq_out.clear(); short_h = nullptr; diff --git a/igc.cc b/igc.cc index 3ab9fef0a..44a187405 100644 --- a/igc.cc +++ b/igc.cc @@ -37,7 +37,6 @@ #include // for foreach, qPrintable #include "defs.h" -#include "cet_util.h" // for cet_convert_init #include "gbfile.h" // for gbfprintf, gbfclose, gbfopen, gbfputs, gbfgetstr, gbfile #include "src/core/datetime.h" // for DateTime @@ -109,9 +108,6 @@ static igc_rec_type_t get_record(char** rec) char* c; retry: *rec = c = gbfgetstr(file_in); - if ((lineno++ == 0) && file_in->unicode) { - cet_convert_init(CET_CHARSET_UTF8, 1); - } if (c == nullptr) { return rec_none; } diff --git a/main.cc b/main.cc index 52bcb9028..299406f82 100644 --- a/main.cc +++ b/main.cc @@ -44,7 +44,6 @@ #endif #include "defs.h" -#include "cet_util.h" // for cet_convert_init, cet_convert_deinit #include "csv_util.h" // for csv_linesplit #include "filter.h" // for Filter #include "filter_vecs.h" // for FilterVecs @@ -343,15 +342,11 @@ run(const char* prog_name) global_opts.masked_objective |= WPTDATAMASK; } - cet_convert_init(ivecs->get_encode(), ivecs->get_fixed_encode()); /* init by module vec */ - start_session(ivecs->get_name(), fname); ivecs->rd_init(fname); ivecs->read(); ivecs->rd_deinit(); - cet_convert_deinit(); - did_something = true; break; case 'F': @@ -366,13 +361,10 @@ run(const char* prog_name) global_opts.masked_objective |= WPTDATAMASK; } - cet_convert_init(ovecs->get_encode(), ovecs->get_fixed_encode()); - ovecs->wr_init(ofname); ovecs->write(); ovecs->wr_deinit(); - cet_convert_deinit(); } break; case 's': @@ -543,33 +535,25 @@ run(const char* prog_name) /* reinitialize xcsv in case two formats that use xcsv were given */ (void) Vecs::Instance().find_vec(ivecs->get_argstring()); - cet_convert_init(ivecs->get_encode(), 1); - start_session(ivecs->get_name(), qargs.at(0)); ivecs->rd_init(qargs.at(0)); ivecs->read(); ivecs->rd_deinit(); - cet_convert_deinit(); - if (qargs.size() == 2 && ovecs) { /* reinitialize xcsv in case two formats that use xcsv were given */ (void) Vecs::Instance().find_vec(ovecs->get_argstring()); - cet_convert_init(ovecs->get_encode(), 1); - ovecs->wr_init(qargs.at(1)); ovecs->write(); ovecs->wr_deinit(); - cet_convert_deinit(); } } else if (!qargs.isEmpty()) { usage(prog_name,0); return 0; } if (ovecs == nullptr) { - cet_convert_init(CET_CHARSET_ASCII, 1); auto waypt_disp_lambda = [&fbOutput](const Waypoint* wpt)->void { fbOutput.waypt_disp(wpt); }; diff --git a/nmea.cc b/nmea.cc index 431e505bf..fc63c610a 100644 --- a/nmea.cc +++ b/nmea.cc @@ -42,7 +42,6 @@ #include "defs.h" #include "nmea.h" -#include "cet_util.h" // for cet_convert_init #include "gbfile.h" // for gbfprintf, gbfflush, gbfclose, gbfopen, gbfgetstr, gbfile #include "gbser.h" // for gbser_set_speed, gbser_flush, gbser_read_line, gbser_deinit, gbser_init, gbser_write #include "jeeps/gpsmath.h" // for GPS_Lookup_Datum_Index, GPS_Math_Known_Datum_To_WGS84_M @@ -989,10 +988,6 @@ NmeaFormat::read() while ((ibuf = gbfgetstr(file_in))) { line++; - if ((line == 0) & file_in->unicode) { - cet_convert_init(CET_CHARSET_UTF8, 1); - } - if ((line == 0) && (case_ignore_strncmp(ibuf, "@SonyGPS/ver", 12) == 0)) { /* special hack for Sony GPS-CS1 files: they are fully (?) nmea compatible, but come with a header line like diff --git a/util.cc b/util.cc index 643c86244..e7d0e6767 100644 --- a/util.cc +++ b/util.cc @@ -1346,6 +1346,15 @@ int gb_ptr2int(const void* p) return x.i; } +QTextCodec* get_codec(const QByteArray& cs_name) +{ + QTextCodec* codec = QTextCodec::codecForName(cs_name); + if (codec == nullptr) { + fatal(FatalMsg().nospace() << "Unsupported character set " << cs_name << "."); + } + return codec; +} + void list_codecs() { -- 2.30.2